<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章类别</title>
    <link rel="stylesheet" href="/assets/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/assets/css/article/articleCategory.css">

</head>

<body>
    <div class="layui-card">
        <div class="layui-card-header">文章类别管理 <button type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                id="addArticleSort">添加类别</button></div>
        <div class="layui-card-body">
            <table class="layui-table">
                <colgroup>

                    <col>
                    <col>
                    <col width="200">
                </colgroup>
                <thead>
                    <tr>

                        <th>分类名称</th>
                        <th>分类别名</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
    <script type="text/html" id="tp1">
        {{each data}}
        <tr>
            
            <td>{{$value.name}}</td>
            <td>{{$value.alias}}</td>
            <td>
                <button type="button" class="layui-btn layui-btn layui-btn-sm" id="modifyBnt" data-id="{{$value.id}}">编辑</button>
                <button type="button" class="layui-btn layui-btn-danger layui-btn layui-btn-sm" id="delBnt" data-id="{{$value.id}}">删除</button>
                
            </td>
        </tr>
        {{/each}}
    </script>
    <script type="text/html" id="display-add">
        <form class="layui-form" lay-filter="formData" id="addForm">
            <div class="layui-form-item">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-block">
                <input type="text" name="name" required  lay-verify="required|name" placeholder="请输入标题" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">分类别名</label>
              <div class="layui-input-block">
                <input type="text" name="alias" required  lay-verify="required|alias" placeholder="请输入标题" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit  >立即提交</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>        
    </script>
    <script type="text/html" id="display-modify">
        <form class="layui-form" lay-filter="modifyForm" id="modifyForm">
            <input type="hidden" name="id">
            <div class="layui-form-item">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-block">
                <input type="text" name="name" required  lay-verify="required|name" placeholder="请输入标题" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">分类别名</label>
              <div class="layui-input-block">
                <input type="text" name="alias" required  lay-verify="required|alias" placeholder="请输入标题" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit  >立即提交</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>        
    </script>
    <script src="/assets/lib/layui/layui.all.js"></script>
    <script src="/assets/lib/jquery.js"></script>
    <script src="/assets/js/baseApi.js"></script>
    <script src="/assets/lib/template-web.js"></script>
    <script>
        $(function () {
            const { form, layer } = layui;
            //获取文章列表
            function getUserInfo() {
                $.ajax({
                    type: 'get',
                    url: '/my/article/cates',
                    success(res) {
                        console.log(res);
                        if (res.status === 0) {
                            const html = template('tp1', { data: res.data });
                            $("tbody").html(html);
                        } else {
                            layer.msg('文章列表获取失败')
                        }
                    }
                })
            }
            getUserInfo();

            //添加分类
            $("#addArticleSort").on('click', function () {
                layer.open({
                    title: '添加文章分类',
                    type: 1,
                    content: $("#display-add").html(),
                    area: ['500px', '250px']
                });
            })
            //表单验证
            form.verify({
                name: function (value) {
                    if (value.length >= 10) {
                        return '分类名称长度为1-10';
                    }
                },
                alias: function (value) {
                    if (value.length >= 10) {
                        return '分类呢称长度为1-10';
                    }
                }
            })
            //提交分类            
            $("body").on('submit', '#addForm', function (e) {
                e.preventDefault();
                console.log(form.val('formData'))
                $.ajax({
                    type: 'post',
                    url: '/my/article/addcates',
                    data: form.val('formData'),
                    success: function (response) {
                        console.log(response);
                        if (response.status === 0) {
                            layer.msg(response.message)
                            getUserInfo();
                        } else {
                            layer.msg(response.message)
                        }
                    }
                })
            })
            //删除按钮
            $("body").on('click', '#delBnt', function () {
                console.log($(this).attr('data-id'));
                const id = $(this).attr('data-id');
                layer.confirm('您确定删除该分类吗?', function (index) {
                    //do something
                    $.ajax({
                        type: 'get',
                        url: '/my/article/deletecate/' + id,
                        success(res) {
                            // console.log(res);
                            layer.msg(res.message)
                            if (res.status === 0) {
                                getUserInfo();
                            }
                        }
                    })
                    layer.close(index);
                });
            })
            let formId= null;
            //编辑按钮
            $("body").on('click', '#modifyBnt', function () {
                console.log($(this).attr('data-id'));
                const id = $(this).attr('data-id');
                formId = layer.open({
                    title: '修改文章分类',
                    type: 1,
                    content: $("#display-modify").html(),
                    area: ['500px', '250px']
                });
                $.ajax({
                    type: 'get',
                    url: '/my/article/cates/' + id,
                    success(res) {
                        console.log(res);
                        if (res.status !== 0) {
                            return layer.msg(res.message)
                        }
                        form.val('modifyForm', res.data)
                    }
                })

            })
            //编辑分类信息提交
            $("body").on('submit', function (e) {
                e.preventDefault();
                console.log('11111111');
                const formData = form.val('modifyForm');
                console.log(formData);
                $.ajax({
                    type:'post',
                    url:'/my/article/updatecate',
                    data:formData,
                    success(res){
                        console.log(res);
                        layer.msg(res.message);
                        if(res.status === 0){
                            getUserInfo();
                            layer.close(formId)
                        }
                    }
                })
            })
        })
    </script>
</body>

</html>